package com.dyzx.hbb.common.config;


import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * OpenAPI 3.0 配置类
 */
@Configuration
public class OpenApiConfig {
    
    private String buildApiDescription() {
        return """
               ## API使用指南
               
               ### 认证方式
               - 所有接口需在Header中添加: `Authorization: Bearer {token}`
               
               ### 通用响应格式
               ```json
               {
                 "code": 200,
                 "message": "success",
                 "data": {}
               }
               ```
               
               ### 错误代码
               | 代码 | 说明          |
               |------|-------------|
               | 400  | 参数错误       |
               | 401  | 未认证        |
               | 403  | 权限不足       |
               | 500  | 服务器内部错误  |
               """;
    }

    /**
     * /**
     * 系统管理接口文档
     */
    @Bean
    public GroupedOpenApi authApi() {
        return GroupedOpenApi.builder()
                .group("网页授权")
                //  .pathsToMatch("/auth/**")
                .pathsToMatch("/auth/**")
                //.packagesToScan("com.dyzx.hbb.security")
                .build();
    }

    @Bean
    public GroupedOpenApi sysApi() {
        return GroupedOpenApi.builder()
                .group("系统接口")
                .pathsToMatch("/sys/**")
                // .packagesToScan("com.dyzx.hbb.modules.sys")
                .build();
    }
    @Bean
    public GroupedOpenApi smsApi() {
        return GroupedOpenApi.builder()
                .group("短信接口")
                .pathsToMatch("/sms/**")
                // .packagesToScan("com.dyzx.hbb.modules.sys")
                .build();
    }
    @Bean
    public GroupedOpenApi fcxsApi() {
        return GroupedOpenApi.builder()
                .group("房源接口")
                .pathsToMatch("/fcxs/**")
                // .packagesToScan("com.dyzx.hbb.modules.sys")
                .build();
    }

    @Bean
    public GroupedOpenApi wmsApi() {
        return GroupedOpenApi.builder()
                .group("库存接口")
                .pathsToMatch("/wms/**")
                // .packagesToScan("com.dyzx.hbb.modules.sys")
                .build();
    }

    @Bean
    public GroupedOpenApi financeApi() {
        return GroupedOpenApi.builder()
                .group("财务接口")
                .pathsToMatch("/finance/**")
                // .packagesToScan("com.dyzx.hbb.modules.sys")
                .build();
    }

    @Bean
    public GroupedOpenApi mtnApi() {
        return GroupedOpenApi.builder()
                .group("车辆接口")
                .pathsToMatch("/mtn/**")
                // .packagesToScan("com.dyzx.hbb.modules.sys")
                .build();
    }

    @Bean
    public OpenAPI openAPI() {
        // 配置安全认证
        SecurityScheme securityScheme = new SecurityScheme()
                .type(SecurityScheme.Type.HTTP)
                .scheme("bearer")
                .bearerFormat("JWT")
                .name("Authorization")
                .in(SecurityScheme.In.HEADER);

        SecurityRequirement securityRequirement = new SecurityRequirement().addList("bearerAuth");

        return new OpenAPI()
                .components(new Components().addSecuritySchemes("bearerAuth", securityScheme))
                .addSecurityItem(securityRequirement)
                .info(new Info()
                        .title("汽车管理系统API文档")
                        .version("1.0.0")
                        .description(buildApiDescription())
                        .contact(new Contact()
                                .name("技术支持")
                                .email("support@carmanager.com")
                                .url("https://www.carmanager.com")))
                .addServersItem(new Server().url("/").description("开发环境"))
                .addSecurityItem(new SecurityRequirement().addList("Bearer"))
                .addSecurityItem(new SecurityRequirement()
                        .addList("Bearer")
                        .addList("token")
                        .addList("terminal"))
                .components(new Components()
                        .addSecuritySchemes("Bearer", new SecurityScheme()
                                .type(SecurityScheme.Type.HTTP)
                                .scheme("bearer")
                                .bearerFormat("JWT")
                                .description("JWT认证：请在下框中输入JWT token（不需要输入Bearer前缀）")
                                .in(SecurityScheme.In.HEADER)
                                .name("Authorization"))
                        .addSecuritySchemes("terminal", new SecurityScheme()
                                .type(SecurityScheme.Type.MUTUALTLS)
                                .in(SecurityScheme.In.HEADER)
                                .name("terminal")
                                .description("终端类型")))
                .addServersItem(new Server().url("/").description("开发环境"));
    }
}